﻿using System.Collections.Generic;
using BlawkayEngine;
using BlawkayEngine.Input;
using BlawkayEngine.ScreenSystem;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace BlawkayHockey
{
    public class OptionsSettingsScreen : Screen
    {
        public OptionsSettingsScreen(Game oGame, Section oSection = (Section)100)
            : base(oGame)
        {
            _Texture_Background = oGame.Content.Load<Texture2D>("Essentials\\Pics\\Square");
            _Font_General = oGame.Content.Load<SpriteFont>("Essentials\\Fonts\\Debug"); ;

            _AudioOptions = new List<string>();
            for (AudioOptions oOption = AudioOptions.Music; oOption <= AudioOptions.Back; oOption++)
                _AudioOptions.Add(oOption.ToString().Replace('_', ' '));

            _VisualOptions = new List<string>();
            for (VisualOptions oOption = VisualOptions.Brightness; oOption <= VisualOptions.Back; oOption++)
                _VisualOptions.Add(oOption.ToString().Replace('_', ' '));

            _DefaultOptions = new List<string>();
            for (DefaultOptions oOption = DefaultOptions.Time; oOption <= DefaultOptions.Back; oOption++)
                _DefaultOptions.Add(oOption.ToString().Replace('_', ' '));

            _OptionIndex = 0;

            if (oSection == (Section)100)
                _CurrentSection = Section.Audio;
            else
                _CurrentSection = oSection;

            SwitchSection();
        }

        public override void Update(ScreenManager oManager)
        {
            KeyboardInput oInput = GameInfo.KeyboardInput;

            if (oInput.JustPressed(Keys.E))
            {
                _CurrentSection--;
                if (_CurrentSection < Section.Audio)
                    _CurrentSection = Section.Defaults;
                SwitchSection();
            }
            if (oInput.JustPressed(Keys.R))
            {
                _CurrentSection++;
                if (_CurrentSection >= Section.Back)
                    _CurrentSection = Section.Audio;
                SwitchSection();
            }

            if (oInput.JustPressed(Keys.Up))
            {
                _OptionIndex--;
                if (_OptionIndex < 0)
                    _OptionIndex = _CurrentList.Count - 1;
            }
            if (oInput.JustPressed(Keys.Down))
            {
                _OptionIndex++;
                if (_OptionIndex > _CurrentList.Count - 1)
                    _OptionIndex = 0;
            }

            if (oInput.JustPressed(Keys.LeftShift, Keys.RightShift))
            {
                GoToOptionsMenu(oManager);
            }
        }

        public override void Draw(ScreenManager oManager, SpriteBatch oSB)
        {
            Rectangle oBackground = GameInfo.Resolution;
            oBackground.Width = 800;
            oBackground.X = oBackground.Center.X - (oBackground.Width / 2);
            oBackground.Height -= 100;
            oBackground.Y = 50;

            Vector2 oPosition = new Vector2(oBackground.Left + 50, oBackground.Top + 50);

            oSB.Draw(_Texture_Background, oBackground, null, Color.Black * 0.25f * Transition, 0.0f, Vector2.Zero, SpriteEffects.None, 0.0f);

            int i = 0;
            foreach (string oString in _CurrentList)
            {
                Vector2 oMeausrement = _Font_General.MeasureString(oString);
                Color oColor = i == _OptionIndex ?
                    Color.Yellow : Color.White;

                oSB.DrawString(_Font_General, oString, oPosition, oColor * Transition, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 1.0f);

                oPosition.Y += oMeausrement.Y;
                i++;
            }
        }

        public override void ActionWhenFullyAdded(ScreenManager oManager)
        {

        }

        public override void ActionWhenFullyRemoved(ScreenManager oManager)
        {
            if (_GoingToOptionsMenu)
            {
                // Options Screen should already be there. If not, add one.
                bool oHasOptionsMenuScreen = false;
                foreach (Screen oScreen in oManager.Screens)
                {
                    if (oScreen is OptionsMenuScreen)
                    {
                        oHasOptionsMenuScreen = true;
                        break;
                    }
                }

                if (oHasOptionsMenuScreen == false)
                {
                    oManager.AddScreen(new OptionsMenuScreen(oManager.Game));
                }
            }
        }

        bool _GoingToOptionsMenu = false;

        private void GoToOptionsMenu(ScreenManager oManager)
        {
            oManager.RemoveScreen(this);
            _GoingToOptionsMenu = true;
        }

        private void SwitchSection()
        {
            switch (_CurrentSection)
            {
                case Section.Audio: _CurrentList = _AudioOptions; break;
                case Section.Visual: _CurrentList = _VisualOptions; break;
                case Section.Defaults: _CurrentList = _DefaultOptions; break;
                default: _CurrentList = _AudioOptions; break;
            }

            _OptionIndex = 0;
        }

        private Texture2D _Texture_Background;
        private SpriteFont _Font_General;

        private List<string> _AudioOptions;
        private List<string> _VisualOptions;
        private List<string> _DefaultOptions;
        
        private Section _CurrentSection;
        private List<string> _CurrentList;
        private int _OptionIndex;


        public enum Section
        {
            Audio,
            Visual,
            Defaults,
            Back
        }

        public enum AudioOptions
        {
            Music,
            Menu_Sounds,
            Game_Sounds,
            Announcers,
            Crowd,
            Back
        }

        public enum VisualOptions
        {
            Brightness,
            Contrast,
            Stretch,
            Back
        }

        public enum DefaultOptions
        {
            Time,
            Difficulty,
            Back
        }










    }
}
